package com.sky.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sky.constant.MessageConstant;
import com.sky.exception.BaseException;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;

/**
 * @author pm
 * date: 2024/5/18 15:54
 * Description:
 */
@Data
@AllArgsConstructor
@Slf4j
public class BaiduLoadUtil {

    private String address;
    private String output;
    private String ak;
    private String callback;

    public static String URL = "https://api.map.baidu.com/geocoding/v3/?";

    public static String URL2 = "https://api.map.baidu.com/direction/v2/riding?";


    public Integer loadResult(String address){

        HashMap<String, String> baiduMap = new HashMap<>();
        baiduMap.put("address", "北京市海淀区上地十街10号");
        baiduMap.put("output", "json");
        baiduMap.put("ak", "uvBDXXAPWKrb8IPgvHTkiuuzKygW9JND");
//        baiduMap.put("callback", "showLocation");

        String s = HttpClientUtil.doGet(URL, baiduMap);

        JSONObject jsonObject = JSON.parseObject(s);

        if (!jsonObject.getString("status").equals("0")){
            throw new BaseException(MessageConstant.PARSE_ERROR);
        }

        JSONObject object = (JSONObject) jsonObject.get("result");

        JSONObject data = (JSONObject) object.get("location");

        String origin = data.get("lat").toString()+","+ data.get("lng").toString();



        HashMap<String, String> otherMap = new HashMap<>();
        otherMap.put("address", address);
        otherMap.put("output", "json");
        otherMap.put("ak", "uvBDXXAPWKrb8IPgvHTkiuuzKygW9JND");
//        baiduMap.put("callback", "showLocation");

        String s2 = HttpClientUtil.doGet(URL, otherMap);

        JSONObject jsonObject2 = JSON.parseObject(s2);

        if (!jsonObject2.getString("status").equals("0")){
            throw new BaseException(MessageConstant.PARSE_ERROR);
        }
        JSONObject objectOther = (JSONObject) jsonObject2.get("result");

        JSONObject dataOther = (JSONObject) objectOther.get("location");

        String destination = dataOther.get("lat").toString()+","+ dataOther.get("lng").toString();
        log.info("字符串:{}",destination);


        HashMap<String, String> lineMap = new HashMap<>();
        lineMap.put("origin",origin);
        lineMap.put("destination", destination);
        lineMap.put("output", "json");
        lineMap.put("ak", "uvBDXXAPWKrb8IPgvHTkiuuzKygW9JND");
//        lineMap.put("riding_type", "1");
//        baiduMap.put("callback", "showLocation");

        String line = HttpClientUtil.doGet(URL2, lineMap);

        JSONObject jsonLine = JSON.parseObject(line);

        if (jsonLine.getString("status").equals("2002")){
            throw new BaseException(jsonLine.getString("message"));
        }

        if (!jsonLine.getString("status").equals("0")){
            throw new BaseException(MessageConstant.UNKNOWN_ERROR);
        }

        JSONObject objectLine = (JSONObject) jsonLine.get("result");
        JSONArray objectLine2 = (JSONArray) objectLine.get("routes");
        JSONObject objectLine3 = (JSONObject) objectLine2.get(0);
        Integer result = (Integer) objectLine3.get("distance");

        return result;
    }
}
